<template>
    <div>
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-Form" label-width="100px">
            <el-form-item label="请输入账号" prop="username">
              <el-input v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item label="请输入密码" prop="password">
              <el-input v-model="loginForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item class="btns">
              <el-button type="primary" @click="login">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(isValid => {
        // console.log(isValid)
        if (!isValid) return false
        if (this.loginForm.username === 'admin' && this.loginForm.password === '123456') {
          localStorage.isLogin = true
          this.$router.push('/product')
        } else {
          this.$message.error('登录失败')
        }
      })
    }
  }
}
</script>

<style scope>
.btns {
    display: flex;
    justify-content: center;
}
</style>
